import 'package:dio/dio.dart';
import 'package:getx_demo_2/http/client.dart';
import 'package:getx_demo_2/http/interceptor.dart';
import 'package:getx_demo_2/http/constants.dart';

class NetWorkManager {
  factory NetWorkManager() => _getInstance();

  static RestClient get instance => _getInstance().client;
  static NetWorkManager? _instance;
  late Dio dio;
  late RestClient client;
  late BaseOptions options;

  NetWorkManager._internal() {
    dio = Dio()
      ..options = BaseOptions(
          baseUrl: Constants.BASE_URL,
          connectTimeout: 10000,
          receiveTimeout: 1000 * 60 * 60 * 24,
          responseType: ResponseType.json,
          headers: {"Content-Type": "application/json"})
      //网络状态拦截
      ..interceptors.add(AuthInterceptor())
      ..interceptors.add(HttpLog())
      ..interceptors.add(ErrorInterceptor());
    client = RestClient(dio);
  }

  static NetWorkManager _getInstance() {
    _instance ??= NetWorkManager._internal();
    return _instance!;
  }
}
